package 算法.leetcode.labuladong.数组和链表;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lchenglong
 * @date 2022/6/16
 */
public class 无重复的最长子串 {
    class Solution {
        public int lengthOfLongestSubstring(String s) {
            Map<Character,Integer> window = new HashMap<>();
            int left=0,right=0;
            int ans = 0;
            while (right<s.length()){
                // 扩大窗口
                char c = s.charAt(right);
                right++;
                window.put(c,window.get(c)==null?1:window.get(c)+1);
                // 存在重复的子串,缩小窗口
                while (window.get(c)>1){
                    char d = s.charAt(left);
                    left++;
                    window.put(d,window.get(d)==null?0:window.get(d)-1);
                }
                ans =  Math.max(ans,right-left);
            }
            return ans;
        }
    }
}
